package tcl.loan.system.utls;

import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.UnavailableSecurityManagerException;
import org.apache.shiro.session.InvalidSessionException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;



import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

import tcl.loan.system.entity.User;
import tcl.loan.system.mapper.UserMapper;
import tcl.loan.system.utls.Strings;
/**
 * Created by linjianding on 2016/12/7.
 */
public class UserUtils{
        public final static java.lang.String salt = "_BOCOMDigest200411241423";
    private static final char[] a = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    private static String jdField_new = "some random value JKLMNOP_!#";
    public static final String bytes2HexStr(byte[] var0) {
        StringBuffer var1 = new StringBuffer(var0.length * 2);

        for(int var2 = 0; var2 < var0.length; ++var2) {
            var1.append(a[var0[var2] >>> 4 & 15]);
            var1.append(a[var0[var2] & 15]);
        }

        return var1.toString();
    }
    public static final String digest(String source) {
        String var1 = jdField_new;
        String var2 = null;

        try {
            MessageDigest var3 = MessageDigest.getInstance("MD5");
            var3.update(source.getBytes("UTF-8"));
            var3.update(var1.getBytes("UTF-8"));
            var3.update("Copyright Client Server International Inc.".getBytes("UTF-8"));
            byte[] var4 = var3.digest();
            var2 = bytes2HexStr(var4);
        } catch (NoSuchAlgorithmException var5) {
            var2 = null;
        } catch (Exception var6) {
            var2 = null;
        }
        return var2;
    }
        @Autowired
       private static UserMapper userMapper  ;
        public static String getCurrentUserName() {
            Subject subject= SecurityUtils.getSubject();
            Object object=subject.getPrincipal();
            String userName = (String)object;
            if (userName != null) {
                return userName;
            } else {
                return null;
            }
        }

    public static User getUser() {
        User user = null;//(User)getCache(CACHE_USER);
        if (user == null) {
            try {
                String userName = (String) SecurityUtils.getSubject().getPrincipal();
                if (userName != null) {
                    user=new User();
                    user.setUserName(userName);
                    user=userMapper.selectOne(user);
                }
            } catch (UnavailableSecurityManagerException e) {

            } catch (InvalidSessionException e) {

            }
        }
        if (user == null) {
            user = new User();
            try {
                SecurityUtils.getSubject().logout();
            } catch (UnavailableSecurityManagerException e) {

            } catch (InvalidSessionException e) {

            }
        }

        return user;
    }


}
